package com.promis.helper;

import com.promis.util.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by jidanbo on 2018/1/16.
 */

public class PersonHelper {

    public static String hidePhone(String phone) {
        if (!StringUtils.isNullOrEmpty(phone)) {

            if (phone.length() == 11) {
                return phone.substring(0, 3) + "****" + phone.substring(7, 11);
            } else if(phone.length() > 4) {
                int count = phone.length() - 4;
                String p = "";
                for (int i = 0; i < count; i++) {
                    p += "*";
                }
                return p + phone.substring(phone.length() - 4, phone.length());
            }
        }
        return phone;
    }

    public static String getAge(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        return getAge(calendar.getTime());
    }

    public static String getTime(int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);

        return getAge(calendar.getTime());
    }

    public static String getAge(Date date) {
        long currentTime = System.currentTimeMillis();
        long time = date.getTime();

        long diffTime = currentTime - time;

        long y = diffTime / (365 * 24 * 60 * 60 * (long) 1000);
        if (y > 0) return y + "周岁";

        long m = diffTime / (30 * 24 * 60 * 60 * (long) 1000);
        if (m > 0) return m + "个月";

        long d = diffTime / (24 * 60 * 60 * (long) 1000);
        if (d > 0) return d + "天";
        else if (diffTime > 0) return "1天";

        return "";
    }
}
